<?php

class IndexController extends Zend_Controller_Action
{

    protected $_redirector = null;

    public function init()
    {
        $this->view->title = "";
        $this->view->indexctrl = true;

        $this->_redirector = $this->_helper->getHelper("Redirector");

        @session_start();
        if(isset($_SESSION['user'])) $this->view->isLogin = true;
        else $this->view->isLogin = false;
    }

    public function indexAction()
    {
       $fid = $this->getRequest()->getParam("fid", 0);

       $floor_db = new Application_Model_DbTable_Floor();
       $this->view->floors = $floor_db->getAll();
       if($fid > 0){
           $this->view->floor = $floor_db->getFloor($fid);
       }
           
       $venue_db = new Application_Model_DbTable_Venue();
       $this->view->venues = $venue_db->getVenues($fid);
    }

    public function loginAction()
    {
        if ($this->getRequest()->isPost()) {
            $postData = $this->getRequest()->getPost();
            
            // get user
            $user_db = new Backend_Model_DbTable_User();
            $user = $user_db->getUserByEmail($postData['email']);

            if($user){
                //session_start();
                $_SESSION['user'] = $user;
            }
        }

        $this->_redirector->gotoUrl('/index/');
    }

    public function logoutAction()
    {
        //session_start();
        if(isset($_SESSION['user'])){
            unset ($_SESSION['user']);
        }

        $this->_redirector->gotoUrl('/index/');
    }

    public function statusAction()
    {
        // check session
        //session_start();
        if($this->view->isLogin){
            $this->view->session = $_SESSION['user'];
        }

        $form = new Application_Form_UserLogin();
        $this->view->form = $form;
    }

    public function getvenueAction()
    {
        $this->_helper->layout()->disableLayout();

        if ($this->getRequest()->isPost()) {
            $postData = $this->getRequest()->getPost();

            // get venue
            $venue_db = new Application_Model_DbTable_Venue();
            $this->view->venue = $venue_db->getVenue($postData['vid']);

            //get user id
            if($this->view->isLogin){
                $this->view->uid = $_SESSION['user']['user_id'];
            }
            else{
                $this->view->uid = '';
            }
        }
    }

    public function bookingAction()
    {
        $this->_helper->layout->disableLayout();

        if ($this->getRequest()->isPost()) {
            $postData = $this->getRequest()->getPost();

            echo "<script>alert('book venue #".$postData['vid']."')</script>";
        }
    }

    public function calendarAction()
    {
        $this->_helper->layout->disableLayout();
        
        $vid = $this->getRequest()->getParam('vid');
        $month = $this->getRequest()->getParam('month');
        $year = $this->getRequest()->getParam('year');

        // Initialize array
        $numofdays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        $occupied = array();
        for($i=1;$i<=$numofdays;$i++) $occupied[$i] = false;

        // Get occupied venue in the month
        $bdate_db = new Application_Model_DbTable_BookingDate();
        $bdates = $bdate_db->getBookingDateInMonth($vid, $month, $year);

        foreach ($bdates as $val){
            $date = date("j",  strtotime($val['date']));
            $occupied[$date] = true;
        }

        // Get month name and first day of the month
        $time = mktime(0,0,0,$month,1,$year);
        $this->view->first = date("w",$time);
        $this->view->month = date("F",$time);
        $this->view->year = $year;

        $this->view->occupied = $occupied;
        $this->view->numofdays = $numofdays;
    }


}















